/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mvc.example.presentation.view;

import javax.swing.event.EventListenerList;
import mvc.example.presentation.events.DataEvent;
import mvc.example.presentation.listeners.StudentDataListener;
import mvc.example.bussines.domain.Student;
import mvc.example.bussines.domain.StudentStatus;
import mvc.example.presentation.controller.StudentController;
import mvc.example.presentation.listeners.StudentActionListener;
import mvc.example.presentation.model.StudentsModel;

/**
 *
 * @author fernando.hernandez
 */
public final class StudentView extends javax.swing.JFrame{

    /**
     * Creates new form NewJFrame
     */
    private StudentController controller;
    private Handler handler = null;
    private EventListenerList listenerList = new EventListenerList();
    private boolean edit;
    
    public StudentView(StudentController controller) {
        initComponents();
        this.controller = controller;
        installListeners();
        changeCurrentStudent(this.controller.getStudentModel().getSelectedStudent());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextLegajo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextApellido = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextDNI = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jButtonGuardar = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(390, 121));
        setMinimumSize(new java.awt.Dimension(390, 121));
        setPreferredSize(new java.awt.Dimension(390, 121));
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jPanel1.setLayout(new java.awt.GridLayout(4, 2, 2, 2));

        jLabel4.setText("Legajo");
        jPanel1.add(jLabel4);

        jTextLegajo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextLegajoActionPerformed(evt);
            }
        });
        jPanel1.add(jTextLegajo);

        jLabel1.setText("Nombre");
        jPanel1.add(jLabel1);
        jPanel1.add(jTextName);

        jLabel2.setText("Apellido");
        jPanel1.add(jLabel2);
        jPanel1.add(jTextApellido);

        jLabel3.setText("DNI");
        jPanel1.add(jLabel3);

        jTextDNI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextDNIActionPerformed(evt);
            }
        });
        jPanel1.add(jTextDNI);

        getContentPane().add(jPanel1);

        jPanel2.setMaximumSize(new java.awt.Dimension(90, 200));
        jPanel2.setMinimumSize(new java.awt.Dimension(90, 200));
        jPanel2.setPreferredSize(new java.awt.Dimension(90, 200));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.PAGE_AXIS));

        jButtonGuardar.setText("Guardar");
        jButtonGuardar.setMaximumSize(new java.awt.Dimension(88, 23));
        jButtonGuardar.setMinimumSize(new java.awt.Dimension(88, 23));
        jButtonGuardar.setPreferredSize(new java.awt.Dimension(88, 23));
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });
        jPanel2.add(jButtonGuardar);

        jButtonCancel.setText("Cancelar");
        jButtonCancel.setMaximumSize(new java.awt.Dimension(88, 23));
        jButtonCancel.setMinimumSize(new java.awt.Dimension(88, 23));
        jButtonCancel.setPreferredSize(new java.awt.Dimension(88, 23));
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });
        jPanel2.add(jButtonCancel);

        getContentPane().add(jPanel2);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextDNIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextDNIActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextDNIActionPerformed

    private void jTextLegajoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextLegajoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextLegajoActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
       this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        Student student = null;
        if(!controller.isEdit()){
            student = new Student();
            student.setDni(jTextDNI.getText());
            student.setFirstname(jTextName.getText());
            student.setLastname(jTextApellido.getText());
            student.setStudentId(jTextLegajo.getText());
            student.setStatus(StudentStatus.REGULAR);
        }else{
            student = controller.getStudentModel().getSelectedStudent();
             student.setDni(jTextDNI.getText());
                student.setFirstname(jTextName.getText());
                student.setLastname(jTextApellido.getText());
                student.setStudentId(jTextLegajo.getText());
            }
        fireSaveEvent(student);
    }//GEN-LAST:event_jButtonGuardarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextApellido;
    private javax.swing.JTextField jTextDNI;
    private javax.swing.JTextField jTextLegajo;
    private javax.swing.JTextField jTextName;
    // End of variables declaration//GEN-END:variables

    private void fireSaveEvent(Student student) {
        StudentActionListener[] listeners = listenerList.getListeners(StudentActionListener.class);
        for (int i = 0; i < listeners.length; i++) {
            StudentActionListener studentActionListener = listeners[i];
            DataEvent dataEvent = new DataEvent(student, DataEvent.SAVE_DATA, -1);
            studentActionListener.saveStudent(dataEvent);
        }
    }
    
    private class Handler implements 
            StudentDataListener {

        @Override
        public void contentsChanged(DataEvent e) {
            changeCurrentStudent(((StudentsModel)e.getSource()).getSelectedStudent());
        }
    }
     public void changeCurrentStudent(Student selectedStudent) {
        if(selectedStudent!=null){
            this.jTextApellido.setText(selectedStudent.getLastname());
            this.jTextDNI.setText(selectedStudent.getDni());
            this.jTextName.setText(selectedStudent.getFirstname());
            this.jTextLegajo.setText(selectedStudent.getStudentId());
        }else{
           
            this.jTextApellido.setText("");
            this.jTextDNI.setText("");
            this.jTextName.setText("");
            this.jTextLegajo.setText("");
        }
     }
    private  Handler getHandler(){
        if(handler == null){
            handler = new Handler();
        }
        return handler;
    }
    private void installListeners(){
        controller.getStudentModel().addDataListener(getHandler());
        
        listenerList.add(StudentActionListener.class, controller);
    }
}
